<?php
class menusController extends AppController {

	var $name = 'menus';
	var $helpers = array('Html', 'Form' );

	var $recursion = 2;

	function index($section = "*")
  	{
  	$data = $this->Menu->findAllThreaded(array('section_id'=>$section), null, 'Menu.position');
    
	if(isset($this->params['requested'])) {
			return $data;
		}
	
	$this->set('data', $data);
  }

	function view($id = null) {
		if(!$id) {
			$this->Session->setFlash('Invalid Menu.');
			$this->redirect(array('action'=>'index'), null, true);
		}
		$this->set('menu', $this->Menu->read(null, $id));
	}

	function add() {
		if(!empty($this->data)) {
			$this->cleanUpFields();
			$this->Menu->create();
			if($this->Menu->save($this->data)) {
				$this->Session->setFlash('The Menu has been saved');
				$this->redirect(array('action'=>'index'), null, true);
			} else {
				$this->Session->setFlash('The Menu could not be saved. Please, try again.');
			}
		}
		$sections = $this->Menu->Section->generateList();
		$this->set(compact('sections'));
	}

	function edit($id = null) {
		if(!$id && empty($this->data)) {
			$this->Session->setFlash('Invalid Menu');
			$this->redirect(array('action'=>'index'), null, true);
		}
		if(!empty($this->data)) {
			$this->cleanUpFields();
			if($this->Menu->save($this->data)) {
				$this->Session->setFlash('The Menu saved');
				$this->redirect(array('action'=>'index'), null, true);
			} else {
				$this->Session->setFlash('The Menu could not be saved. Please, try again.');
			}
		}
		if(empty($this->data)) {
			$this->data = $this->Menu->read(null, $id);
		}
		$sections = $this->Menu->Section->generateList();
		$this->set(compact('sections'));
	}

	function delete($id = null) {
		if(!$id) {
			$this->Session->setFlash('Invalid id for Menu');
			$this->redirect(array('action'=>'index'), null, true);
		}
		if($this->Menu->del($id)) {
			$this->Session->setFlash('Menu #'.$id.' deleted');
			$this->redirect(array('action'=>'index'), null, true);
		}
	}


	function admin_index() {
		$this->Menu->recursive = 0;
		$this->set('menus', $this->paginate());
	}

	function admin_view($id = null) {
		if(!$id) {
			$this->Session->setFlash('Invalid Menu.');
			$this->redirect(array('action'=>'index'), null, true);
		}
		$this->set('menu', $this->Menu->read(null, $id));
	}

	function admin_add() {
		if(!empty($this->data)) {
			$this->cleanUpFields();
			$this->Menu->create();
			if($this->Menu->save($this->data)) {
				$this->Session->setFlash('The Menu has been saved');
				$this->redirect(array('action'=>'index'), null, true);
			} else {
				$this->Session->setFlash('The Menu could not be saved. Please, try again.');
			}
		}
		$sections = $this->Menu->Section->generateList();
		$this->set(compact('sections'));
	}

	function admin_edit($id = null) {
		if(!$id && empty($this->data)) {
			$this->Session->setFlash('Invalid Menu');
			$this->redirect(array('action'=>'index'), null, true);
		}
		if(!empty($this->data)) {
			$this->cleanUpFields();
			if($this->Menu->save($this->data)) {
				$this->Session->setFlash('The Menu saved');
				$this->redirect(array('action'=>'index'), null, true);
			} else {
				$this->Session->setFlash('The Menu could not be saved. Please, try again.');
			}
		}
		if(empty($this->data)) {
			$this->data = $this->Menu->read(null, $id);
		}
		$sections = $this->Menu->Section->generateList();
		$this->set(compact('sections'));
	}

	function admin_delete($id = null) {
		if(!$id) {
			$this->Session->setFlash('Invalid id for Menu');
			$this->redirect(array('action'=>'index'), null, true);
		}
		if($this->Menu->del($id)) {
			$this->Session->setFlash('Menu #'.$id.' deleted');
			$this->redirect(array('action'=>'index'), null, true);
		}
	}

}
?>